<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索与添加 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="searchVal"
            clearable
            @clear="clearSearch"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchGoodsBtn"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表表格 -->
      <el-table :data="goodsLists.goods" style="width: 100%" stripe border>
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="70">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70">
        </el-table-column>
        <el-table-column label="创建日期" width="150">
          <template slot-scope="spoce">
            {{ spoce.row.add_time | formatData }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="spoce">
            <el-button
              size="mini"
              type="primary"
              class="el-icon-edit"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              class="el-icon-delete"
              @click="delGoods(spoce.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="goodsListsParams.pagenum"
        :page-sizes="[10, 50, 100, 200]"
        :page-size="goodsListsParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="goodsLists.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //   获取商品列表数据
      goodsLists: {},
      //   获取商品列表数据参数
      goodsListsParams: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 搜索输入框值
      searchVal: ''
    }
  },
  created() {
    this.getGoodsLists()
  },
  methods: {
    async getGoodsLists() {
      const { data } = await this.$http.get('goods', {
        params: this.goodsListsParams
      })
      if (data.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      // this.$message.error('获取商品列表成功')
      console.log('获取商品列表成功')
      this.goodsLists = data.data
      console.log(this.goodsLists)
    },
    //  分页处理函数
    handleCurrentChange(val) {
      this.goodsListsParams.pagenum = val
      this.getGoodsLists()
    },
    handleSizeChange(val) {
      this.goodsListsParams.pagesize = val
      this.getGoodsLists()
    },
    //  搜索商品按钮
    searchGoodsBtn() {
      this.goodsListsParams.query = this.searchVal
      this.getGoodsLists()
    },
    //  清除搜索按钮输入框
    clearSearch() {
      this.goodsListsParams.query = this.searchVal
      this.getGoodsLists()
    },
    //  删除商品
    delGoods(row) {
      console.log(row)
      this.$confirm(`此操作将永久 ${row.goods_id} 删除, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data } = await this.$http.delete(`goods/${row.goods_id}`)
          if (data.meta.status !== 200) {
            return this.$message.error('删除商品失败')
          }
          console.log('获取商品列表成功')
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getGoodsLists()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 添加商品
    addGoods() {
      this.$router.push('addGoods')
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 10px;
}
</style>
